package com.t2c.tibetantochinese.regandlogin;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.base.BaseActivity;
import com.t2c.tibetantochinese.common.HttpRequestManager;
import com.t2c.tibetantochinese.moudle.Login;
import com.t2c.tibetantochinese.utils.CommonUtil;
import com.t2c.tibetantochinese.utils.IntentUtil;
import com.t2c.tibetantochinese.utils.SPUtil;
import com.t2c.tibetantochinese.views.TitleBar;

/**
 * Created by zoulu on 16/10/26.
 * 登录
 */

public class LoginActivity extends BaseActivity{
    private EditText et_name;
    private EditText et_pass;
    private Button register;
    private Button btn;

    @Override
    protected int getContentViewResId() {
        return R.layout.login_layout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView(){
        TitleBar titleBar = (TitleBar)findViewById(R.id.titlebar);
        titleBar.setOnLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_name = (EditText) findViewById(R.id.et_name);
        et_pass = (EditText) findViewById(R.id.et_pass);
        btn = (Button) findViewById(R.id.btn);
        register = (Button) findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.gotoRegister(LoginActivity.this);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isLegal()){
                    login();
                }else{
                    Toast.makeText(LoginActivity.this,"账号或密码不能为空",Toast.LENGTH_SHORT).show();
                }
            }
        });

        if(CommonUtil.isChinese(this)) {
            titleBar.setTitle(getResources().getString(R.string.login));
            et_name.setHint(getResources().getString(R.string.myaccount));
            et_pass.setHint(getResources().getString(R.string.password));
            register.setText(getResources().getString(R.string.register));
            btn.setText(getResources().getString(R.string.login));
        }else{
            titleBar.getTitle().setTypeface(CommonUtil.getTibetanType(this));
            titleBar.setTitle(getResources().getString(R.string.tlogin));
            et_name.setTypeface(CommonUtil.getTibetanType(this));
            et_pass.setTypeface(CommonUtil.getTibetanType(this));
            register.setTypeface(CommonUtil.getTibetanType(this));
            btn.setTypeface(CommonUtil.getTibetanType(this));
            et_name.setHint(getResources().getString(R.string.tmyaccount));
            et_pass.setHint(getResources().getString(R.string.tpassword));
            register.setText(getResources().getString(R.string.tregister));
            btn.setText(getResources().getString(R.string.tlogin));
        }
    }

    private void login(){
        HttpRequestManager.getInstance().login(new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("========", "onResponse: "+response );
                Login login = new Gson().fromJson(response,Login.class);
                if(login.isSuccess()) {
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    SPUtil.put(LoginActivity.this,"name",login.getData().getUsername());
                    IntentUtil.gotoMain(LoginActivity.this);
                    finish();
                }else{
                    Toast.makeText(LoginActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(LoginActivity.this,"服务器出问题啦",Toast.LENGTH_SHORT).show();
            }
        }, et_name.getText().toString(),et_pass.getText().toString()).commit();
    }

    private boolean isLegal(){
        return !TextUtils.isEmpty(et_name.getText().toString())
                && !TextUtils.isEmpty(et_pass.getText().toString());
    }
}
